package com.tony.mindhr.exception;

import com.tony.mindhr.model.RespBean;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: GlobalExceptionHandler
 * @Auther: 皮蛋布丁
 * @Date: 2021/05/25/22:23
 * @Description: 全局异常处理
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(SQLException.class)
    public RespBean sqlException(SQLException e) {
        if (e instanceof SQLIntegrityConstraintViolationException) {
            return RespBean.error("该数据存在关联数据，操作失败!");
        }
        return RespBean.error("数据库异常，操作失败!");
    }


}
